package com.byxm.wlgw.control;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;




import org.apache.commons.beanutils.PropertyUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;


import com.byxm.wlgw.bean.GwcxxBean;
import com.byxm.wlgw.bean.ShxxBean;
import com.byxm.wlgw.bean.YhxxBean;
import com.byxm.wlgw.service.GwcxxService;
import com.byxm.wlgw.service.ShxxService;
import com.opensymphony.xwork2.ActionContext;

public class GwcxxAction extends BaseAction{
	private static final long serialVersionUID = 1L;
	
	//用户信息ID
	private String yhxxid="";
	//商品信息ID
	private String spxxid="";
	//商品型号ID
	private String spxhid="";
	//商品数量
	private String spsl="";
	//添加flag
	private String flag;
	//购物车信息list
	private String gwcxxid="";
	//接受下单结账时，传来的修改商品数量的JSON
	private String jsonObj="";
	
	//购物车总金额
	private float gwczje=0;

	private Map<String, Object> session; 
	
	//收货信息
	private List<ShxxBean> lstShxx;
	
	//购物车信息list
	List<GwcxxBean> lstGwcxx=null;

	GwcxxService gwcxxService=new GwcxxService();
	ShxxService shxxService=new ShxxService();
	/**
	 * 获取购物车信息
	 */
	public String list(){
		session=ActionContext.getContext().getSession();
		YhxxBean yhxxBean=(YhxxBean)session.get("yhxxSession");
		if(session.get("yhxxSession")==null){
			return "input";
		}else{
			try {
				lstGwcxx=gwcxxService.getGwcxx(yhxxBean.getYhxxid());
			} catch (Exception e) {
				e.printStackTrace();
			}
			gwczje=getGwcxxJsje(lstGwcxx);
			return "success_01";
		}
		
	}

	/**
	 * 添加购物车信息
	 * @return
	 * @throws Exception 
	 */
	public String save() throws Exception{
		session= ActionContext.getContext().getSession();
		GwcxxBean gwcxx=this.getGwcxx();
		//添加信息到购物车
		if(gwcxxService.getGwcxxByxtsp(gwcxx)){//检查有没有相同商品
			//添加购物车信息进去
			gwcxxService.addGwcxxSpsl(gwcxx);
			flag="1";
		}else{
		   flag=gwcxxService.addSptogwc(gwcxx)+"";
		}
		// this.updateOrAddGwcxxSession();
		return SUCCESS;
	}
	/**
	 * 删除购物车信息的方法
	 * @return
	 */
	public String del(){
		session= ActionContext.getContext().getSession();
		
		List<GwcxxBean> lstGwcxx=new ArrayList<GwcxxBean>();
		try {
			//删除购物车信息
			gwcxxService.deleteGwcxx(this.getGwcxx());
		} catch (Exception e) {
			e.printStackTrace();
		}
		//删除后更新一下Session
		 this.updateOrAddGwcxxSession();
		 flag="1";
		return SUCCESS;
	}
	/**
	 * 更新购物车信息
	 * @return
	 * @throws Exception 
	 * @throws NumberFormatException 
	 */
	public String update() throws NumberFormatException, Exception{
		List<GwcxxBean> lstGwcxx=null;
	    session= ActionContext.getContext().getSession();
	    //前台传回来的 json数组 
		JSONArray jsonArray = new JSONArray(jsonObj);
		//当前session里的购物车
		lstGwcxx=(List<GwcxxBean>) session.get("gwcxxSession");
		for(int i=0;i<lstGwcxx.size();i++){//他们两个相同的  
			JSONObject jsonObj = jsonArray.getJSONObject(i);//json数组i下标
			if((lstGwcxx.get(i).getGwcxxid()+"").equalsIgnoreCase(jsonObj.get("gwcxxid")+"")&&!(lstGwcxx.get(i).getSpsl()+"").equalsIgnoreCase(jsonObj.get("spsl")+"")){
				//更新一下商品数量
				gwcxxService.updateGwcxx(Integer.parseInt(jsonObj.get("gwcxxid")+""), Integer.parseInt(jsonObj.get("spsl")+""));
			}
		}
		//更新一下 购物车session
		this.updateOrAddGwcxxSession();
		//flag="1";
		return "success_05";
	}
	
	
	/********************私有方法区*****************/
	/**
	 *更新购物车session 以及总金额 session
	 */

	private void updateOrAddGwcxxSession(){
		session= ActionContext.getContext().getSession();
		List<GwcxxBean> lstGwcxx;
		try {
			lstGwcxx = gwcxxService.getGwcxx(this.getGwcxx().getYhxxid());
			session.put("gwcxxSession", lstGwcxx);//放购物车信息
			session.put("gwcxxJsjeSession", this.getGwcxxJsje(lstGwcxx));//结算金额
			session.put("gwcxxLength", lstGwcxx.size());
		} catch (Exception e) {
			e.printStackTrace();
		};

	}
	
	/**
	 * 获得购物车结算总金额
	 */
	private float getGwcxxJsje(List<GwcxxBean> lstGwcxx){
		float fjsje=0;
		for(int i=0;i<lstGwcxx.size();i++){
			fjsje+=(lstGwcxx.get(i).getSpsj())*(lstGwcxx.get(i).getSpsl());
		}
		return fjsje;
	}
	
	/**
	 * 获得从前台传来的数据
	 */
	private GwcxxBean getGwcxx(){
		GwcxxBean gwcxx=new GwcxxBean();
		if(yhxxid.trim()!=""){
			gwcxx.setYhxxid(Integer.parseInt(yhxxid));
		}
		if(spxxid.trim()!=""){
			gwcxx.setSpxxid(Integer.parseInt(spxxid));
		}
		if(spxhid.trim()!=""){
			gwcxx.setSpxhid(Integer.parseInt(spxhid));
		}
		if(spsl.trim()!=""){
			gwcxx.setSpsl(Integer.parseInt(spsl));
		}
		if(gwcxxid.trim()!=""){
			gwcxx.setGwcxxid(Integer.parseInt(gwcxxid));
		}
		return gwcxx;
	}
	
	
	
	/**************setter getter********************/
	public String getYhxxid() {
		return yhxxid;
	}
	public void setYhxxid(String yhxxid) {
		this.yhxxid = yhxxid;
	}
	public String getSpxxid() {
		return spxxid;
	}
	public void setSpxxid(String spxxid) {
		this.spxxid = spxxid;
	}
	public String getSpxhid() {
		return spxhid;
	}
	public void setSpxhid(String spxhid) {
		this.spxhid = spxhid;
	}
	public String getSpsl() {
		return spsl;
	}
	public void setSpsl(String spsl) {
		this.spsl = spsl;
	}
	public String getFlag() {
		return flag;
	}
	public void setFlag(String flag) {
		this.flag = flag;
	}
	public String getGwcxxid() {
		return gwcxxid;
	}
	public void setGwcxxid(String gwcxxid) {
		this.gwcxxid = gwcxxid;
	}
	public String getJsonObj() {
		return jsonObj;
	}
	public void setJsonObj(String jsonObj) {
		this.jsonObj = jsonObj;
	}
	public Map<String, Object> getSession() {
		return session;
	}
	public void setSession(Map<String, Object> session) {
		this.session = session;
	}
	public List<ShxxBean> getLstShxx() {
		return lstShxx;
	}

	public void setLstShxx(List<ShxxBean> lstShxx) {
		this.lstShxx = lstShxx;
	}

	public List<GwcxxBean> getLstGwcxx() {
		return lstGwcxx;
	}

	public void setLstGwcxx(List<GwcxxBean> lstGwcxx) {
		this.lstGwcxx = lstGwcxx;
	}

	public float getGwczje() {
		return gwczje;
	}

	public void setGwczje(float gwczje) {
		this.gwczje = gwczje;
	}
	
}
